﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhSystemBLL.orderData
{
  public   class custTypeBLL
    {
    private   custTypeTableAdapter _custTypeAdapter = null;
    private custTypeTableAdapter custTypeAdapter
    {
        get 
        {
            if (_custTypeAdapter == null) _custTypeAdapter = new custTypeTableAdapter();
            return _custTypeAdapter;
        }
    }
    /// <summary>
    /// 查询客户类别表
    /// </summary>
    /// <param name="row"></param>
    /// <returns></returns>
    public orderDataSet.custTypeDataTable getcustTypeAll()
    {
        try
        {
            return custTypeAdapter.GetDatacustTypeAll();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
    }
    /// <summary>
    /// 根据客户类别查询客户类别表
    /// </summary>
    /// <param name="row"></param>
    /// <returns></returns>
      public orderDataSet .custTypeDataTable getcustTypeAll(string cType)
      {
          try 
          {
              return custTypeAdapter.GetDatacustTypeBycType(cType);
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message.ToString());
          }
      }
      /// <summary>
      /// 插入客户类别表
      /// </summary>
      /// <param name="row"></param>
      /// <returns></returns>
      public int custTypeInsert(custTypeModel row)
      {
          int i = 0;
          SqlConnection conn = dataConn.connOpen();
          SqlTransaction tn = null;

          try
          {
              tn = conn.BeginTransaction();
              custTypeDAL  custType = new custTypeDAL(tn ,conn );
              custType.custTypeInsert(row);
              tn.Commit();
              return i;
          }
          catch (SqlException ex)
          {
              tn.Rollback();
              if (ex.Number == 2627)
              {
                  throw new Exception("这个客户类别已经存在");
              }


              throw new Exception(ex.Number.ToString() + ex.Message.ToString());

          }
          catch (Exception ex)
          {
              tn.Rollback();
              throw new Exception(ex.Message.ToString());

          }
          finally
          {
              conn.Close();
          }
      }
      /// <summary>
      /// 更新客户信息表
      /// </summary>
      /// <param name="row"></param>
      /// <returns></returns>
      public int custTypeUpdate(custTypeModel row)
      {
          int i = 0;
          SqlConnection conn = dataConn.connOpen();
          SqlTransaction tn = null;
          try
          {
              tn = conn.BeginTransaction();
              custTypeDAL custType = new custTypeDAL(tn ,conn );
              custType.custTypeUpdate(row);
              tn.Commit();
              return i;
          }
          catch (Exception ex)
          {
              tn.Rollback();
              throw new Exception(ex.Message.ToString());
              
          }
          finally
          {
              conn.Close();
          }
      }
      /// <summary>
      /// 删除一行客户类别表
      /// </summary>
      /// <param name="row"></param>
      /// <returns></returns>
      public int custTypeDel(string cType)
      {
          int i = 0;
          SqlConnection conn = dataConn.connOpen();
          SqlTransaction tn = null;
          try
          {
              tn = conn.BeginTransaction();
              custTypeDAL custType = new custTypeDAL(tn ,conn );
              custType.custTypeListDel(cType);
              tn.Commit();
              return i;
          }
          catch (Exception ex)
          {
              tn.Rollback();
              throw new Exception(ex.Message.ToString());
              
          }
          finally
          {
              conn.Close();
          }
      }
    }
}
